package com.itheima.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Permission;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/permission")
public class PermissionServlet extends BaseServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = request.getParameter("operation");
        if ("list".equals(operation)) {
            this.list(request, response);
        } else if ("toAdd".equals(operation)) {
            this.toAdd(request, response);
        } else if ("save".equals(operation)) {
            this.save(request, response);
        } else if ("toEdit".equals(operation)) {
            this.toEdit(request, response);
        } else if ("edit".equals(operation)) {
            this.edit(request, response);
        } else if ("delete".equals(operation)) {
            this.delete(request, response);
        } else if ("deleteByIds".equals(operation)) {
            this.deleteByIds(request, response);
        }else if ("findById".equals(operation)) {
            this.findById(request, response);
        }else if ("findByMethodName".equals(operation)) {
            this.findByMethodName(request, response);
        }
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //进入列表页
        //获取数据
        String str = request.getParameter("str");
        if(StringUtils.isNotBlank(str)){
            findByMethodName(request,response);
            return;
        }
        int page = 1;
        int size = 5;
        if (StringUtils.isNotBlank(request.getParameter("page"))) {
            page = Integer.parseInt(request.getParameter("page"));
        }
        if (StringUtils.isNotBlank(request.getParameter("size"))) {
           size = Integer.parseInt(request.getParameter("size"));
        }
        PageInfo all = permissionService.findAll(page, size);
        //将数据保存到指定的位置
        request.setAttribute("page", all);
        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/system/permission/permission-list.jsp").forward(request,response);
        //request.getRequestDispatcher("/pages/permission-list.jsp").forward(request, response);

    }

    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //加载所有的部门信息放入到permissionList
        List<Permission> all = permissionService.findAll();
        //System.out.println(all);
        request.setAttribute("permissionList", all);
        //跳转页面
           request.getRequestDispatcher("/WEB-INF/pages/system/permission/permission-add.jsp").forward(request,response);
        //request.getRequestDispatcher("/pages/permission-add.jsp").forward(request, response);

    }

    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //将数据获取到，封装成一个对象
        Permission permission = BeanUtil.fillBean(request, Permission.class, "yyyy-MM-dd");
        //调用业务层接口save
//        PermissionService permissionService = new PermissionServiceImpl();
        permissionService.save(permission);
        //跳转回到页面list
        //list(request,response);
        response.sendRedirect(request.getContextPath() + "/system/permission?operation=list");
        //response.sendRedirect(request.getContextPath()+"/pages/permission-list.jsp");

    }

    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询要修改的数据findById
        String id = request.getParameter("id");
//        PermissionService permissionService = new PermissionServiceImpl();
        Permission permission = permissionService.findById(id);
        //加载所有的部门信息放入到permissionList
        List<Permission> all = permissionService.findAll();
        request.setAttribute("permissionList", all);
        //将数据加载到指定区域，供页面获取
        request.setAttribute("permission", permission);
        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/system/permission/update.jsp").forward(request, response);
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //将数据获取到，封装成一个对象
        Permission permission = BeanUtil.fillBean(request, Permission.class, "yyyy-MM-dd");
        //调用业务层接口save
//        PermissionService permissionService = new PermissionServiceImpl();
        permissionService.update(permission);
        //跳转回到页面list
        //list(request,response);
        response.sendRedirect(request.getContextPath() + "/system/permission?operation=list");
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //将数据获取到，封装成一个对象
        Permission permission = BeanUtil.fillBean(request, Permission.class);
        //调用业务层接口save
//        PermissionService permissionService = new PermissionServiceImpl();
        permissionService.delete(permission);
        //跳转回到页面list
        response.sendRedirect(request.getContextPath() + "/system/permission?operation=list");
        //response.sendRedirect(request.getContextPath()+"/system/permission?operation=list");
    }

    private void deleteById(HttpServletRequest request, HttpServletResponse response, String id) throws IOException {

        //调用业务层接口save
//        PermissionService permissionService = new PermissionServiceImpl();
        permissionService.deleteById(id);
        //跳转回到页面list
        //response.sendRedirect(request.getContextPath()+"/system/permission?operation=list");
        //response.sendRedirect(request.getContextPath()+"/system/permission?operation=list");
    }

    private void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String ids = request.getParameter("ids");
        //System.out.println(ids);
        String[] split = ids.split(",");
        for (String id : split) {
            //System.out.println(id);
            permissionService.deleteById(id);
        }
        response.sendRedirect(request.getContextPath() + "/system/permission?operation=list");


          /*  //将数据获取到，封装成一个对象
            Permission permission = BeanUtil.fillBean(request,Permission.class);
            //调用业务层接口save
//        PermissionService permissionService = new PermissionServiceImpl();
            permissionService.delete(permission);
            //跳转回到页面list
            response.sendRedirect(request.getContextPath()+"/system/permission?operation=list");
            //转回web-inf下再打开response.sendRedirect(request.getContextPath()+"/system/permission?operation=list");*/
    }
    private void findById(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String id = request.getParameter("id");
        //调用业务层接口save
//        PermissionService permissionService = new PermissionServiceImpl();
        Permission permission = permissionService.findById(id);
        request.setAttribute("permission",permission);
        //跳转回到页面list
        //response.sendRedirect(request.getContextPath()+"/system/permission?operation=list");
        //response.sendRfedirect(request.getContextPath()+"/system/permission?operation=list");
        request.getRequestDispatcher("/WEB-INF/pages/system/permission/permission-show.jsp").forward(request,response);

    }
    public void findByMethodName(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        /*String methodName = request.getParameter("sousuo");
        List<Syslog> all = syslogService.findByMethodName(methodName);

        request.setAttribute("page",all);
        request.getRequestDispatcher("/WEB-INF/pages/system/syslog/syslog-list.jsp").forward(request,response);
*/
        String value = request.getParameter("str");
        String sizeStr = request.getParameter("size");
        int size=Integer.parseInt(sizeStr);
        //System.out.println(value);
        //进入列表页获取数据
        int page = 1;

        if (StringUtils.isNotBlank(request.getParameter("page"))){
            page=Integer.parseInt(request.getParameter("page"));
        }
       /* if(StringUtils.isNotBlank(request.getParameter("size"))){
            size=Integer.parseInt(request.getParameter("size"));
        }*/
        PageInfo all = permissionService.findByMethodName(page, size, value);
        //将数据保存到指位置
        request.setAttribute("page",all);
        request.setAttribute("str",value);
        //跳转页面
        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/system/permission/permission-list.jsp").forward(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

